package com.zdy.im.user.controller;



import java.util.ArrayList;
import java.util.List;

import com.sixeco.framework.common.api.ApiDoc;
import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.common.enums.SysResultCodeEnum;
import com.sixeco.framework.http.data.vo.HttpRequestByIdVo;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.http.handler.OutputResponse;
import com.zdy.im.user.UrlCommand;
import com.zdy.im.user.service.UserInfoService;
import com.zdy.im.user.dto.request.UserInfoRequestDto;
import com.zdy.im.user.dto.response.UserInfoResponseDto;
import com.zdy.im.user.facade.UserInfoFacade;
import com.zdy.im.user.vo.request.*;
import com.zdy.im.user.vo.response.UserInfoSearchResponseVo;
import com.zdy.im.user.vo.response.UserInfoShowResponseVo;
import com.zdy.im.user.vo.response.UserLoginImResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Title: UserInfoController.java
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @version V1.0
 *  <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 * @Description: TODO(说明) 对象缓存统一模板实现类;
 */
@ApiDoc(
        module = "用户表模块", isApp = true
)
@Controller
public class UserInfoController {

    @Autowired
    private UserInfoFacade userInfoFacade;

    @Autowired
    private UserInfoService userInfoService;




    /**
    * @Title: 分页获取用户表信息
    * @Description:testRequestVo @{Link TestRequestVo}
    * @param
    * @return
    * @throw
    * @author suven
    * @date 2019-07-19 13:16:48
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ApiDoc(
            value = "分页获取用户表信息",
            request = UserInfoQueryRequestVo.class,
            response = UserInfoShowResponseVo.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_pageList, method = RequestMethod.GET)
    public void list(OutputResponse out, UserInfoQueryRequestVo userInfoQueryRequestVo){
            UserInfoRequestDto userInfoRequestDto = UserInfoRequestDto.build( ).clone(userInfoQueryRequestVo);

        BasePage page =  BasePage.build().toPageSize(userInfoQueryRequestVo.getPageSize()).toPageNo(userInfoQueryRequestVo.getPageNo());
        page.toParamObject(userInfoRequestDto );
        ResponseResultList<UserInfoResponseDto> resultList = userInfoService.getUserInfoByNextPage(page);

        if(null == resultList || resultList.getList().isEmpty() ){
            out.writeSuccess();
            return;
        }
        List<UserInfoShowResponseVo> listVo = new ArrayList<>();
        resultList.getList().forEach(e -> listVo.add(UserInfoShowResponseVo.build().clone(e)));

        ResponseResultList list =  ResponseResultList.build().toList(listVo)
                .toIsNextPage(resultList.getIsNextPage())
                .toPageIndex(resultList.getPageIndex())
                .toTotal(resultList.getTotal());
        out.write(list);
    }





    /**
    * @Title: 查看用户表信息
    * @Description:testRequestVo @{Link TestRequestVo}
    * @param
    * @return
    * @throw
    * @author suven
    * @date 2019-07-19 13:16:48
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ApiDoc(
            value = "查看用户信息",
            request = HttpRequestByIdVo.class,
            response = UserInfoShowResponseVo.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_info ,method = RequestMethod.GET)
    public void detail(OutputResponse out, HttpRequestByIdVo idRequestVo){

            UserInfoResponseDto userInfoResponseDto = userInfoService.getUserInfoById(idRequestVo.getId());
            UserInfoShowResponseVo vo =  UserInfoShowResponseVo.build().clone(userInfoResponseDto);
        out.write(vo);
    }

    @ApiDoc(
            value = "注册用户",
            request = RegisterRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_register, method = RequestMethod.POST)
    public void register(OutputResponse out, RegisterRequestVo registerRequestVo){
        boolean result = userInfoFacade.register(registerRequestVo);
        out.write(result);
    }


    @ApiDoc(
            value = "用户账号搜索，搜索名称，登陆名称，手机号码",
            request = UserInfoSearchRequestVo.class,
            response = UserInfoSearchResponseVo.class
    )
    @RequestMapping(value = UrlCommand.USER_USERINFO_SEARCH_USER, method = RequestMethod.GET)
    public void searchUser(OutputResponse out, UserInfoSearchRequestVo searchRequestVo){
        ResponseResultList<UserInfoSearchResponseVo>  result = userInfoFacade.searchUser(searchRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "删除某个用户下好友",
            request = DelFriendRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_delFriend,method = RequestMethod.POST)
    public  void  del(OutputResponse out, DelFriendRequestVo delFriendRequestVo){
        if (delFriendRequestVo.getFriendId() == 0 || delFriendRequestVo.getUserId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = userInfoFacade.delFriend(delFriendRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "修改用户信息",
            request = UpUserInfoRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_update,method = RequestMethod.POST)
    public  void  update(OutputResponse out, UpUserInfoRequestVo friendRequestVo){
        if (friendRequestVo.getUserId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = userInfoFacade.upUserInfo(friendRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "修改好友备注",
            request = FriendRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_upFriendRemark,method = RequestMethod.POST)
    public  void  upFriendRemark(OutputResponse out, FriendRequestVo friendRequestVo){
        if (friendRequestVo.getFriendId() == 0 || friendRequestVo.getUserId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = userInfoFacade.upFriendRemark(friendRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "修改用户头像",
            request = UserHeadImgRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_upHeadImg,method = RequestMethod.POST)
    public  void  upHeadImg(OutputResponse out, UserHeadImgRequestVo userHeadImgRequestVo){
        if (userHeadImgRequestVo.getUserId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = userInfoFacade.upHeadImg(userHeadImgRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "添加好友",
            request = GroupFriendRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_imUserFriendShip_save,method = RequestMethod.POST)
    public  void  save(OutputResponse out, GroupFriendRequestVo friendRequestVo){
        if (friendRequestVo.getFriendId() == 0 || friendRequestVo.getUserId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = userInfoFacade.saveUserFriend(friendRequestVo);
        out.write(result);
    }

    @ApiDoc(
            value = "好友迁移其他组",
            request = GroupFriendRequestVo.class,
            response = boolean.class
    )
    @RequestMapping(value = UrlCommand.user_imUserFriendShip_transfer,method = RequestMethod.POST)
    public  void  userTransferGroup(OutputResponse out, GroupFriendRequestVo friendRequestVo){
        if (friendRequestVo.getFriendId() == 0 || friendRequestVo.getUserId() == 0|| friendRequestVo.getGroupId() == 0) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        boolean result = userInfoFacade.userTransferGroup(friendRequestVo);
        out.write(result);
    }


    @ApiDoc(
            value = "用户获取im握手登陆信息接口",
            request = UserLoginImRequestVo.class,
            response = UserLoginImResponseVo.class
    )
    @RequestMapping(value = UrlCommand.user_userInfo_login_im,method = RequestMethod.POST)
    public  void  userLoginIm(OutputResponse out, UserLoginImRequestVo userLoginImRequestVo){
        if (userLoginImRequestVo.getUserId() == 0 || userLoginImRequestVo.getToken() == null) {
            out.write(SysResultCodeEnum.SYS_WEB_ID_INFO_NO_EXIST);
            return ;
        }
        UserLoginImResponseVo vo = userInfoFacade.getUserLoginImWsUrl(userLoginImRequestVo);
        out.write(vo);
    }


}
